package com.dashan.servlets;

import static com.dashan.Constants.*;
import static com.dashan.Constants.CURRENT_ONLY;
import static com.dashan.Constants.FALSE;
import static com.dashan.Constants.FRIENDNAME;
import static com.dashan.Constants.FRIEND_REQUEST_FROM;
import static com.dashan.Constants.FRIEND_REQUEST_MESSAGE;
import static com.dashan.Constants.FRIEND_REQUEST_TO;
import static com.dashan.Constants.GROUPNAME;
import static com.dashan.Constants.NICKNAME;
import static com.dashan.Constants.PERMISSION;
import static com.dashan.Constants.POSITION_ACC;
import static com.dashan.Constants.POSITION_LAT;
import static com.dashan.Constants.POSITION_LNG;
import static com.dashan.Constants.PWDSHA;
import static com.dashan.Constants.RESET;
import static com.dashan.Constants.SALTED_PWDSHA;
import static com.dashan.Constants.SEPERATOR;
import static com.dashan.Constants.SESSIONID;
import static com.dashan.Constants.STATUS;
import static com.dashan.Constants.TRUE;
import static com.dashan.Constants.USERNAME;
import static com.dashan.TestConstants.TEST_FRIENDNAME;
import static com.dashan.TestConstants.TEST_GROUPNAME_1;
import static com.dashan.TestConstants.TEST_GROUPNAME_2;
import static com.dashan.TestConstants.TEST_NICKNAME;
import static com.dashan.TestConstants.TEST_PWDSHA;
import static com.dashan.TestConstants.TEST_USERNAME;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dashan.backend.AccessControlManager.Permission;
import com.dashan.backend.util.HttpRequestSender;


@SuppressWarnings("serial")
public class CleanDatastoreTest extends DashanServlet {
    
    private String address;
    private PrintWriter out;
    private String saltedPwdSha;
    private String sessionid;
    
    @Override
    public String getUrlPattern() {
        return "/cleandatastoretest";
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        
        out.println("<html>");
        out.println("<head>");
        out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
        out.println(String.format("<title>Clean Datastore Test</title>"));
        out.println("</head>");
        
        out.println("<body>");
        
        out.println(String.format("<FORM action=\"%s\" method=\"post\">", getUrlPattern()));
        out.print(String.format("<LABEL style=\"color: red; font-size: 40px\">%s</LABEL><BR>", 
                "This test will remove ALL data!"));
        out.print(String.format("<LABEL for=\"%s\">%s: </LABEL>", "address", "Test against:"));
        out.print(String.format(
                "<INPUT type=\"text\" name=\"%s\" value=\"%s\"></INPUT>", "address", "localhost:8080"));
        out.print("<BR>");
        out.print(String.format("<LABEL style=\"color: red;\">%s</LABEL><BR>", 
        "NOTE: Cannot test against the same server when running in App Engine."));
        out.print(String.format("<LABEL >%s</LABEL><BR>", 
        "Local datastore: \"localhost:8080\""));
        out.print(String.format("<LABEL >%s</LABEL><BR>", 
        "Appengien datastore: \"dashan-net.appspot.com\""));
        out.println("<INPUT type=\"submit\" value=\"Continue\">");
        out.println("</FORM>");
        
        out.println("</body>");
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        address = req.getParameter("address");
        
        out = resp.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
        out.println(String.format("<title>Clean Datastore Test</title>"));
        out.println("</head>");
        
        out.println("<body>");
        
        userTest();
        authenticateTest();
        postPositionStatusTest();
        inviteFriendsTest();
        groupOperationTest();
        permissionOperationTest();
        geoSearchTest();
        userProfileTest();
        
        out.println("</body>");
    }

    private void userTest() throws IOException {
        {
            String servlet = "/vacuumdatabase"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/checkusername"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/createuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(NICKNAME, TEST_NICKNAME);
            sender.addParameter(PWDSHA, TEST_PWDSHA);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/createuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_FRIENDNAME);
            sender.addParameter(NICKNAME, TEST_NICKNAME);
            sender.addParameter(PWDSHA, TEST_PWDSHA);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
    }
    
    private void authenticateTest() throws IOException {
        {
            String servlet = "/getsalt"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
            int start = sender.getRawRes().indexOf("<saltedpwdsha>");
            int end = sender.getRawRes().indexOf("</saltedpwdsha>");
            if (start != -1 && end != -1) {
                saltedPwdSha = sender.getRawRes().substring(start + "<saltedpwdsha>".length(), end);
            } else {
                saltedPwdSha = "0";
            }
        }
        {
            String servlet = "/getsessionid"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SALTED_PWDSHA, saltedPwdSha);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
            int start = sender.getRawRes().indexOf("<sessionid>");
            int end = sender.getRawRes().indexOf("</sessionid>");
            if (start != -1 && end != -1) {
                sessionid = sender.getRawRes().substring(start + "<sessionid>".length(), end);
            } else {
                sessionid = "0";
            }
        }
    }
    
    private void postPositionStatusTest() throws IOException {
        {
            String servlet = "/getuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "1.0");
            sender.addParameter(POSITION_LNG, "1.0");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/poststatus"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            sender.addParameter(STATUS, "I'm Testing!");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/poststatus"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            sender.addParameter(STATUS, "I'm still Testing!");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/getpositionhistory"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);        
        }
        {
            String servlet = "/getstatushistory"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);        
        }
        {
            String servlet = "/getuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);        
        }
    }

    private void inviteFriendsTest() throws IOException {
        {
            String servlet = "/invitefriend"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(FRIEND_REQUEST_TO, TEST_FRIENDNAME);
            sender.addParameter(FRIEND_REQUEST_MESSAGE, "Be my friend!");
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/getpendingfriendinvitation"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_FRIENDNAME);
            sender.addParameter(SESSIONID, "0");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/acceptinvitation"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_FRIENDNAME);
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(FRIEND_REQUEST_FROM, TEST_USERNAME);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/getfriendlist"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
    }

    private void groupOperationTest() throws IOException {
        {
            String servlet = "/createfriendgroup"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/getuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);        
        }
        {
            String servlet = "/deletefriendgroup"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/createfriendgroup"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/createfriendgroup"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_2);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/addfriendtogroup"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(FRIENDNAME, TEST_FRIENDNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/addfriendtogroup"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(FRIENDNAME, TEST_FRIENDNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_2);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/removefriendfromgroup"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(FRIENDNAME, TEST_FRIENDNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/getuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);        
        }
    }
    
    private void permissionOperationTest() throws IOException {
        {
            String servlet = "/changegrouppermission"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(PERMISSION, Permission.CURRENT_POSITION.getName() +
                    SEPERATOR + Permission.PROFILE.getName());
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/getuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);        
        }
        {
            String servlet = "/overridegrouppermission"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(FRIENDNAME, TEST_FRIENDNAME);
            sender.addParameter(PERMISSION, Permission.CURRENT_POSITION.getName());
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/overridegrouppermission"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(GROUPNAME, TEST_GROUPNAME_1);
            sender.addParameter(FRIENDNAME, TEST_FRIENDNAME);
            sender.addParameter(RESET, TRUE);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
    }
    
 // (1*) for current location.
    ///////////////////////////////////////////////////////////////////
    //     (1)112.544718, 37.866892 
    //
    //
    //              (*1)112.548451, 37.864317 
    // (3)112.540855, 37.864080 
    //
    //                                        (3)112.560682, 37.863572 
    //
    //
    //
    //                          (2)112.555447, 37.861980
    //
    //                      (3)112.553301, 37.860387 
    //                       (*2)112.553515, 37.859405 
    //
    //              
    //                                      (*3)112.558150, 37.857914 
    //           (1)112.547078, 37.857406 
    //                                    (1)112.558064, 37.856253
    ///////////////////////////////////////////////////////////////////
    private void geoSearchTest() throws IOException {
        // Create some users.
        {
            String servlet = "/createuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(1)");
            sender.addParameter(NICKNAME, TEST_NICKNAME);
            sender.addParameter(PWDSHA, TEST_PWDSHA);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/createuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(2)");
            sender.addParameter(NICKNAME, TEST_NICKNAME);
            sender.addParameter(PWDSHA, TEST_PWDSHA);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/createuser"; 
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(3)");
            sender.addParameter(NICKNAME, TEST_NICKNAME);
            sender.addParameter(PWDSHA, TEST_PWDSHA);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(1)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.866892");
            sender.addParameter(POSITION_LNG, "112.544718");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(3)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.86408");
            sender.addParameter(POSITION_LNG, "112.540855");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(3)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.863572");
            sender.addParameter(POSITION_LNG, "112.560682");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(2)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.86198");
            sender.addParameter(POSITION_LNG, "112.555447");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(3)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.860387");
            sender.addParameter(POSITION_LNG, "112.553301");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(1)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.857406");
            sender.addParameter(POSITION_LNG, "112.547078");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(1)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.856253");
            sender.addParameter(POSITION_LNG, "112.558064");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(1)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.864317");
            sender.addParameter(POSITION_LNG, "112.548451");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(2)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.859405");
            sender.addParameter(POSITION_LNG, "112.553515");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/postposition";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, "(3)");
            sender.addParameter(SESSIONID, "0");
            sender.addParameter(POSITION_ACC, "1.0");
            sender.addParameter(POSITION_LAT, "37.857914");
            sender.addParameter(POSITION_LNG, "112.55815");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/geosearch";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            sender.addParameter(CURRENT_ONLY, FALSE);
            sender.addParameter(BOUNDING_BOX, "37.862, 37.858, 112.556, 112.545");
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
    }
    
    private void userProfileTest() throws IOException  {
        {
            String xml = "<profile>" +
            "<work>" +
                "<companyName>Microsoft</companyName>" +
            "</work>" +
            "<university>" +
                "<name>ZJU</name>" +
                "<enrollmentYear>2004</enrollmentYear>" +
                "<department>Computer Science</department>" +
            "</university>" +
            "</profile>";
            
            String servlet = "/updateuserprofile";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            sender.addParameter(USER_PROFILE, xml);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
        {
            String servlet = "/getuserprofile";
            HttpRequestSender sender = new HttpRequestSender(address, servlet);
            sender.addParameter(USERNAME, TEST_USERNAME);
            sender.addParameter(SESSIONID, sessionid);
            printTestcase(servlet, sender.getParameters(), sender.sendPost(), out);
        }
    }
    
    private void printTestcase(String servlet, String request, String response, PrintWriter out) { 
        out.print(String.format("<h1>%s</h1>", servlet));
        String[] param = request.split("&");
        
        for (String p : param) {
            out.print(String.format("<div style=\"font-weight:bold;\">%s</div>", p));
        }
        out.print(String.format("<div style=\"font-size: 12px;\">%s</div>", response));
    }
}
